﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Web;

namespace ASPNETPerformance.App_BusinessLogic.Helpers {
    public class CommonHelper {
        #region Cookies
        /// <summary>
        /// Sets cookie
        /// </summary>
        /// <param name="cookieName">Cookie name</param>
        /// <param name="cookieValue">Cookie value</param>
        /// <param name="ts">Timespan</param>
        public static void SetCookie( string cookieName, string cookieValue, TimeSpan ts ) {
            try {
                CookieHelper.SetCookie( cookieName, cookieValue, ts );
            }
            catch( Exception exc ) {
                Debug.WriteLine( exc.Message );
            }
        }

        /// <summary>
        /// Gets cookie string
        /// </summary>
        /// <param name="cookieName">Cookie name</param>
        /// <param name="decode">Decode cookie</param>
        /// <returns>Cookie string</returns>
        public static string GetCookieString( string cookieName, bool decode ) {
            try {
                return CookieHelper.GetCookieValue( cookieName, decode );
            }
            catch {
                return string.Empty;
            }
        }

        /// <summary>
        /// Gets boolean value from cookie
        /// </summary>
        /// <param name="cookieName">Cookie name</param>
        /// <returns>Result</returns>
        public static bool GetCookieBool( string cookieName ) {
            string str1 = GetCookieString( cookieName, true ).ToUpperInvariant();
            return ( str1 == "TRUE" || str1 == "YES" || str1 == "1" );
        }

        /// <summary>
        /// Gets integer value from cookie
        /// </summary>
        /// <param name="cookieName">Cookie name</param>
        /// <returns>Result</returns>
        public static int GetCookieInt( string cookieName ) {
            string str1 = GetCookieString( cookieName, true );
            if( !String.IsNullOrEmpty( str1 ) )
                return Convert.ToInt32( str1 );
            else
                return 0;
        }
        #endregion

        #region Url
        /// <summary>
        /// Gets a value indicating whether current connection is secured
        /// </summary>
        /// <returns>true - secured, false - not secured</returns>
        public static bool IsCurrentConnectionSecured() {
            bool useSSL = false;
            if( HttpContext.Current != null && HttpContext.Current.Request != null ) {
                useSSL = HttpContext.Current.Request.IsSecureConnection;
            }

            return useSSL;
        }

        /// <summary>
        /// Gets this page name
        /// </summary>
        /// <returns></returns>
        public static string GetThisPageURL( bool includeQueryString ) {
            string URL = string.Empty;
            if( HttpContext.Current == null )
                return URL;

            if( includeQueryString ) {
                bool useSSL = IsCurrentConnectionSecured();
                string DemosHost = GetDemosHost( useSSL );
                if( DemosHost.EndsWith( "/" ) )
                    DemosHost = DemosHost.Substring( 0, DemosHost.Length - 1 );
                URL = DemosHost + HttpContext.Current.Request.RawUrl;
            }
            else {
                URL = HttpContext.Current.Request.Url.GetLeftPart( UriPartial.Path );
            }
            return URL;
        }

        /// <summary>
        /// Gets Demos location
        /// </summary>
        /// <returns>Demos location</returns>
        public static string GetDemosLocation() {
            bool useSSL = IsCurrentConnectionSecured();
            return GetDemosLocation( useSSL );
        }

        /// <summary>
        /// Gets Demos location
        /// </summary>
        /// <param name="UseSSL">Use SSL</param>
        /// <returns>Demos location</returns>
        public static string GetDemosLocation( bool UseSSL ) {
            string result = GetDemosHost( UseSSL );
            if( result.EndsWith( "/" ) )
                result = result.Substring( 0, result.Length - 1 );
            result = result + HttpContext.Current.Request.ApplicationPath;
            if( !result.EndsWith( "/" ) )
                result += "/";

            return result;
        }

        /// <summary>
        /// Gets Demos host location
        /// </summary>
        /// <param name="UseSSL">Use SSL</param>
        /// <returns>Demos host location</returns>
        public static string GetDemosHost( bool UseSSL ) {
            string result = "http://" + ServerVariables( "HTTP_HOST" );
            if( !result.EndsWith( "/" ) )
                result += "/";

            if( UseSSL ) {
                if( !String.IsNullOrEmpty( ConfigurationManager.AppSettings[ "SharedSSL" ] ) ) {
                    result = ConfigurationManager.AppSettings[ "SharedSSL" ];
                }
                else {
                    result = result.Replace( "http:/", "https:/" );
                }
            }

            if( !result.EndsWith( "/" ) )
                result += "/";

            return result;
        }

        /// <summary>
        /// Gets server variable by name
        /// </summary>
        /// <param name="Name">Name</param>
        /// <returns>Server variable</returns>
        public static string ServerVariables( string Name ) {
            string tmpS = String.Empty;
            try {
                if( HttpContext.Current.Request.ServerVariables[ Name ] != null ) {
                    tmpS = HttpContext.Current.Request.ServerVariables[ Name ].ToString();
                }
            }
            catch {
                tmpS = String.Empty;
            }
            return tmpS;
        }

        private static string _RelativeWebRoot;
        /// <summary>
        /// Gets the relative root of the website.
        /// </summary>
        /// <value>A string that ends with a '/'.</value>
        public static string RelativeWebRoot {
            get {
                if( _RelativeWebRoot == null )
                    _RelativeWebRoot = VirtualPathUtility.ToAbsolute( "~/" );

                return _RelativeWebRoot;
            }
        }

        #endregion
    }
}